<template>
  <el-container class="home_container">
    <!-- 头部区域 -->
    <el-header>
      <div class="left">
        <img src="../assets/img/home/heima.png" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button class="logout" type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 内容主题 -->
    <el-container>
      <el-aside :width="isShow ? '64px':'200px'">
        <!-- 菜单折叠 -->
        <div class="isShow" @click="btnShow">|||</div>
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409eff"
          :unique-opened="true"
          :collapse="isShow"
          :collapse-transition="false"
          router
          :default-active="checkPath"
        >
          <!-- //一级菜单 -->
          <el-submenu :index="item.id+ ''" v-for="item in MenuList" :key="item.id">
            <template slot="title">
              <i :class="iconObject[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="''+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="savePath(''+subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  name: "Home",
  data() {
    return {
      //左侧菜单数据
      MenuList: [],
      iconObject: {
        125: "iconfont icon-user",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      isShow: false,
      checkPath: "",
    };
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    //折叠菜单显示与否
    btnShow() {
      this.isShow = !this.isShow;
    },
    //获取所有左侧菜单数据
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      // console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg);
      } else {
        this.MenuList = res.data;
        // console.log(this.MenuList);
      }
    },
    // 将子菜单path存到sessionStorage中
    savePath(path) {
      window.sessionStorage.setItem("checkPath", path);
      this.checkPath = path;
    },
  },
  created() {
    this.getMenuList();
    this.checkPath = window.sessionStorage.getItem("checkPath");
  },
  components: {},
  computed: {},
  beforeMount() {},
  mounted() {},
  watch: {},
};
</script>
<style lang="less" scoped>
.home_container {
  height: 100%;
}
.isShow {
  text-align: center;
  color: #ffffff;
  font-size: 10px;
  line-height: 24px;
  letter-spacing: 0.2em;
  background-color: #4a5064;
  cursor: pointer;
}
.el-header {
  background-color: aqua;
  color: #ffffff;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  font-size: 20px;
  .left {
    display: flex;
    align-items: center;
    span {
      margin-left: 20px;
    }
  }
}
.el-aside {
  background-color: #545c64;
  .el-menu {
    border: none;
  }
}
.el-main {
  background-color: bisque;
}
.iconfont {
  margin-right: 10px;
}
</style>
